package com.sopt.eat.views;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.sopt.eat.R;
import com.sopt.eat.db.EatDBHelper;
import com.sopt.eat.vo.SearchItem;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryAdapter extends BaseAdapter {

	private Context mContext;
	private List<SearchItem> mMemberList;
	private int mLayout;
	private MapView historyMapView;
	private ChoicedItemizedOverlay choicedItemizedOverlay;

	public HistoryAdapter(Context context, int layout,
			List<SearchItem> inputList, ChoicedItemizedOverlay choicedItemizedOverlay, MapView curMapView) {
		mContext = context;
		mMemberList = inputList;
		mLayout = layout;
		this.historyMapView = curMapView;
		this.choicedItemizedOverlay = choicedItemizedOverlay;
	}

	public int getCount() {
		return mMemberList.size();
	}

	public Object getItem(int position) {
		return mMemberList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	protected void setMarkToMap(SearchItem searchItem) {
		Double lat = searchItem.getLatitude()*1E6;
		Double lot = searchItem.getLongitude()*1E6;
		GeoPoint p = new GeoPoint(lat.intValue(), lot.intValue());
		
		historyMapView.getController().animateTo(p);
		historyMapView.getController().setZoom(historyMapView.getMaxZoomLevel()-1);
		
		OverlayItem overlayitem = new OverlayItem(p, searchItem
				.getTitle(), "");

		historyMapView.getOverlays().clear();

		choicedItemizedOverlay = new ChoicedItemizedOverlay(mContext.getResources()
				.getDrawable(R.drawable.markerchoiced));

		choicedItemizedOverlay.addOverlay(overlayitem);

		historyMapView.getOverlays().add(choicedItemizedOverlay);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final SearchItem mRow = mMemberList.get(position);
		LayoutInflater inflate = LayoutInflater.from(mContext);

		View mCurView = inflate.inflate(mLayout, parent, false);
		
		TextView title = (TextView)mCurView.findViewById(R.id.title);
		Button phone = (Button) mCurView.findViewById(R.id.phone);
		TextView regdate = (TextView)mCurView.findViewById(R.id.regdate);
		
		title.setText(mRow.getTitle());
		phone.setText(mRow.getPhonenumber());
		regdate.setText(mRow.getRegdate());
		
		phone.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mContext.startActivity(
						new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
								+ mRow.getPhonenumber())));
			}
		});
		
		Button view = (Button) mCurView.findViewById(R.id.historyViewButton);
		view.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Toast.makeText(mContext, mRow.getTitle(), Toast.LENGTH_LONG).show();
				setMarkToMap(mRow);
			}
		});
		
		Button del = (Button) mCurView.findViewById(R.id.historyDeleteButton);
		del.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				AlertDialog alert = new AlertDialog.Builder(mContext)
				.setTitle(R.string.do_you_want_delete)
				.setMessage( mRow.getTitle())
				.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						EatDBHelper helper = new EatDBHelper(mContext);
						helper.deleteBySeq(mRow.getSeq());
						
						Toast.makeText(mContext, mRow.getTitle()+R.string.deleted, Toast.LENGTH_SHORT).show();
						
						mMemberList.remove(mRow);
						notifyDataSetChanged();
					}
				})
				.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						
					}
				}).show();
			}
		});

		return mCurView;
	}
}
